//
//  JKCollectionViewCarousel.m
//  JKXib
//
//  Created by 王冲 on 2019/7/10.
//  Copyright © 2019 王冲. All rights reserved.
//

#import "JKCollectionViewCarousel.h"
@interface JKCollectionViewCarousel()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    
}

@property (nonatomic,strong)UICollectionView * collectionView;
//轮播图总数量
@property (nonatomic,assign) NSInteger totalItems;
@property (nonatomic, weak) NSTimer *timer;

//当前item
@property (nonatomic,assign)NSInteger  currentIndex;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

@end
@implementation JKCollectionViewCarousel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(300, 200);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 50, 300, 200) collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.pagingEnabled = YES;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:self.collectionView];
        self.totalItems = 1000;
        self.flowLayout = flowLayout;
    }
    return self;
}
-(void)setupTimer{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void)invalidateTimer{
    [self.timer invalidate];
    self.timer = nil;
    
}
-(void)automaticScroll{
    
    [self setupCurrentIndex];
    self.currentIndex = self.currentIndex+1;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

-(void)setupCurrentIndex{
    
    if (self.flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
        self.currentIndex = (self.collectionView.contentOffset.x + self.flowLayout.itemSize.width*0.5)/self.flowLayout.itemSize.width;
    }else if (self.flowLayout.scrollDirection == UICollectionViewScrollDirectionVertical){
        
        self.currentIndex = (self.collectionView.contentOffset.y + self.flowLayout.itemSize.height*0.5)/self.flowLayout.itemSize.height;
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1000;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath ];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
    return cell;

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self invalidateTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setupTimer];
}

@end
